﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class FlowmetersSetting : Form
    {
        public int intIndex;
        public int DeviceSelectNo;

        public FlowmetersSetting(int index, int deviceSelectNo)
        {
            InitializeComponent();
            intIndex = index;
            DeviceSelectNo = deviceSelectNo;
            if (DeviceSelectNo == 1)
            {
                labLs1.Visible = false;
                nud3.Visible = false;
                labLs2.Visible = false;
                nud4.Visible = false;
            }
            SetValue();
        }

        /// <summary>
        /// 设置流量计配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    nud5.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.FlowmeterBaseLevel1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelDeviation1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelRatio1.Reverse<byte>().ToArray(), 0) / 10000.0);
                    nud3.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedDeviation1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud4.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedRatio1.Reverse<byte>().ToArray(), 0) / 10000.0);
                }

                if (intIndex == 1)
                {
                    nud5.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.FlowmeterBaseLevel2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelDeviation2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelRatio2.Reverse<byte>().ToArray(), 0) / 10000.0);
                    nud3.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedDeviation2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    nud4.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedRatio2.Reverse<byte>().ToArray(), 0) / 10000.0);
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取流量计配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.FlowmeterBaseLevel1 = BitConverter.GetBytes(Convert.ToInt32(nud5.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelDeviation1 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelRatio1 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 10000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedDeviation1 = BitConverter.GetBytes(Convert.ToInt32(nud3.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedRatio1 = BitConverter.GetBytes(Convert.ToUInt32(nud4.Value * 10000)).Reverse<byte>().ToArray();
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.FlowmeterBaseLevel2 = BitConverter.GetBytes(Convert.ToInt32(nud5.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelDeviation2 = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowWaterLevelRatio2 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 10000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedDeviation2 = BitConverter.GetBytes(Convert.ToInt32(nud3.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowCurrentSpeedRatio2 = BitConverter.GetBytes(Convert.ToUInt32(nud4.Value * 10000)).Reverse<byte>().ToArray();
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
           GetValue();
            this.Dispose();
        }
    }
}
